import requests
import csv
import os, sys, time, json
import datetime

from pyquery import PyQuery as pq
from tomorrow import threads
import random
#当前文件的路径
pwd = os.getcwd()
sys.path.append(pwd)
project_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")
sys.path.append(project_path)
print(project_path)

from api.rest_api import RestApi

from util.util_function import CheckDir, DownloadFile, WriteInfo
from util.log_handler import LogHandler
from util.config import GetConfig

# log = LogHandler('read_csv')

# 获取文件名
def get_filename(path):
	filename = os.path.basename(path)
	filename = filename.split('.')[0]
	return filename

# 获取当前执行程序的文件名
def get_execute_filename():
	return get_filename(__file__)

log = LogHandler(get_execute_filename())

api = RestApi()

# 调用接口
def post_guatian_data(data):
	try:
		r = api.post_guatian_data(data)
		return (json.loads(r)["data"])
	except Exception as e:
		log.error("api request fail: %s"%(e), exc_info=True)

def deal_guatian_info(list_data):
	for data in list_data:
		id = data['id']
		content = json.dumps(data)
		data = {'id': id, 'content':content}
		print(data['id'], json.loads(data['content'])['pictures'][0])
		post_guatian_data(data)
		# break

def list_file(path, ext='.json'):
    '''
    1.作用:遍历文件夹下的所有指定后缀名的文件的具体路径
    2.参数： 
        path:需要遍历的目录， 
        ext:文件后缀名，默认为'.json'
    3.返回值:文件的绝对路径
    '''
    list_file=[]
    for root, dirs, files in os.walk(path):
        for file in files:
            file_ext = file.endswith(ext)
            if file_ext:
                fpath = os.path.join(root, file)
                list_file.append(fpath)
    return list_file

# 读取文件
def read_json_file(path):
	f = open(path, 'r', encoding='utf8')
	info = json.load(f)
	f.close()
	return info

if __name__ == '__main__':
	list_json_file = list_file('/home/ubuntu/workspace/spider_data/guatian/json_data20191112', '.json')
	for json_file in list_json_file:
		print(json_file)
		json_data = read_json_file(json_file)
		list_data = json_data['payload']['list']
		# print(list_data)
		deal_guatian_info(list_data)

